﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Drawing;
namespace BCL.Tool.Extend
{
    public static class ImageExtend
    {

        public static BitmapImage ToBitmap(this System.IO.Stream st)
        {
            if (st == null)
                return null;
            st.Position = 0;
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.StreamSource = st;
            image.EndInit();
            return image;
        }

        public static BitmapImage ToBitmap(this byte[] buffer)
        {
            if (buffer == null)
                return null;
            return new MemoryStream(buffer).ToBitmap();

        }

        public static Image ToImage(this byte[] buffer)
        {
            if (buffer == null)
                return null;
            return new MemoryStream(buffer).ToImage();
        }
        public static Image ToImage(this Stream buffer)
        {
            if (buffer == null)
                return null;
            buffer.Position = 0;
            return Image.FromStream(buffer);
        }


        public static Stream GetFileStream(string path)
        {
            return new MemoryStream(File.ReadAllBytes(path));
        }


        public static Byte[] ToBytes(this Stream stream)
        {
            if (stream == null)
                return null;
            byte[] buffer = new byte[stream.Length];
            long position = stream.Position;
            stream.Position = 0;
            stream.Write(buffer, 0, buffer.Length);
            stream.Position = position;
            return buffer;
        }
    }

}
